package com.wangzaiplus.test.juc.thread.CyclicBarrier;

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.TimeUnit;

/**
 * @ClassName CyclicBarrierDemo2
 * @Description: TODO
 * @Author wgc
 * @Date 2020/06/30 9:53
 * @Version V1.0
 **/
public class CyclicBarrierDemo2 {
    public  static CyclicBarrier  cyclicBarrier=new CyclicBarrier(2);

    public  static  class  T extends  Thread{
        int sleep;
        public T(String name, int sleep) {
            super(name);
            this.sleep = sleep;
        }

        @Override
        public void run() {
            //模拟休眠
            try {
                TimeUnit.SECONDS.sleep(sleep);
                long starTime = System.currentTimeMillis();
                //调用await()的时候，当前线程将会被阻塞，需要等待其他员工都到达await了才能继续
                cyclicBarrier.await();
                long endTime = System.currentTimeMillis();
                System.out.println(this.getName() + ",sleep:" + this.sleep + " 等待了" + (endTime - starTime) + "(ms),开始吃饭了！");
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (BrokenBarrierException e) {
                e.printStackTrace();
            }
        }
    }
    public static void main(String[] args){
        for (int i = 0; i <=10; i++) {
           new T("员工"+i,i).start();
        }
    }
}
